<?php

/*
Copyright 2005-2011 Textpattern CMS Project
All Rights Reserved.

This file is part of Textpattern.

Textpattern is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('txp'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

// Build config.

if (defined('txp_site_id'))
{
	@$config['db'] = $sites[txp_site_id]['db'];
	if (!empty($sites[txp_site_id]['database_default']))
	{
		$config['database_default'] = $sites[txp_site_id]['database_default'];
	}
	unset($sites);
}

if (empty($config['auth']['database']))
{
	@$config['auth']['database'] = $config['database_default'];
}

define('txp_core_dir', $config['core_dir']);

@$plug_search_paths = $config['plug_search_paths'];
@$plug_cache_dir = $config['plug_cache_dir'];

require($config['sparkplug_dir'].'/sparkplug.php');

// Textpattern application class definition

require($config['core_dir'].'/shared/txp_admin_controller.php');
require($config['core_dir'].'/shared/input.php');

class _TxpAdmin extends SparkApplication
{
	private $_headElements;
	private $_user;
	private $_prefsModel;
	private $_prefs;
	private $_pluginsModel;
	private $_plugins;
	
	//---------------------------------------------------------------------------

	public function __construct($spark, $config)
	{
		parent::__construct($spark, $config);
		self::$lang->load('common');

		$this->pushModelDir($this->config->get('core_dir') . '/admin/models');

		$this->config->set('app_name', 'Textpattern');
		
		// If not yet installed, do the minimum necessarily to let us proceed successfully
		// to the installer.
		
		$this->_headElements = array();
/*		
		TODO:
			This is where we sanity-check the installation, validate the schema version, etc.
*/

		try
		{
			$this->_prefsModel = $this->newModel('Preferences');
			$this->_prefs = $this->_prefsModel->fetchPrefVals();
		}
		catch (Exception $e)
		{
			exit($e->getMessage());
		}

		// register notifications we are interested in
		
		$this->observer->observe(array($this, 'afterLogin'), array('SparkAuthController:login'));
		$this->observer->observe(array($this, 'beforeRender'), array('SparkView:render:before:main'));
		$this->observer->observe(array($this, 'addHeadElement'), array('txp:page:request_add_element:head'));
/*		
		TODO:
			This is where we load Textpattern plugins.
*/
	}
	
	//---------------------------------------------------------------------------

	public function afterLogin()
	{
	}

	//---------------------------------------------------------------------------

	public function beforeRender($message, $view, $varsObj)
	{
		$varsObj->vars['head_elements'] = $this->_headElements;
	}

	//---------------------------------------------------------------------------

	public function addHeadElement($message, $element)
	{
		$this->_headElements[] = $element;
	}

	//---------------------------------------------------------------------------

	public function get_date()
	{
		return gmdate('Y-m-d H:i:s');
	}

	//---------------------------------------------------------------------------

	public function is_installed()
	{
		return $this->config->get('db');
	}

	//---------------------------------------------------------------------------

	public function is_admin()
	{
		return true;
	}

	//---------------------------------------------------------------------------

	public function get_user()
	{
		return $this->_user;
	}

	//---------------------------------------------------------------------------

	public function get_pref($key, $default = NULL)
	{
		return isset($this->_prefs[$key]) ? $this->_prefs[$key] : $default;
	}

	//---------------------------------------------------------------------------
}

// Instantiate the application object.

$app = $spark->manufacture('TxpAdmin', $spark, $config);
unset($config);

// Run it.

$app->run();
